package com.tuanzi.loan.api.controller;

import com.tuanzi.loan.business.entity.shortlink.ShortLink;
import com.tuanzi.loan.business.entity.sms.SmsSendTask;
import com.tuanzi.loan.business.repository.shortlink.ShortLinkRepository;
import com.tuanzi.loan.business.repository.sms.SmsSendTaskRepository;
import com.tuanzi.loan.business.service.shortlink.ShortLinkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Winson
 * @date 2019-02-01
 */

@Slf4j
@RestController
@RequestMapping("/api/shortlink")
public class ShortLinkController {

    @Autowired
    private ShortLinkService shortLinkService;

    @Autowired
    private ShortLinkRepository shortLinkRepository;

    @Autowired
    private SmsSendTaskRepository smsSendTaskRepository;

    private static final Pattern PATTERN = Pattern.compile("https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]");

    @GetMapping("/completeDisplayedShortLink")
    @Deprecated
    // 最好别用了
    public void completeDisplayedShortLink() {
        try {
            List<SmsSendTask> smsSendTasks = smsSendTaskRepository.findAll();
            completeDisplayedShortLink(smsSendTasks);
            log.info("completeDisplayedShortLink success");
        } catch (Exception e) {
            log.error("completeDisplayedShortLink fail", e);
        }
    }

    @GetMapping("/completeShortLink")
    @Deprecated
    // 最好别用了
    public void completeShortLink() {
        try {
            List<SmsSendTask> smsSendTasks = smsSendTaskRepository.findAll();
            completeShortLink(smsSendTasks);
            log.info("completeShortLink success");
        } catch (Exception e) {
            log.error("completeShortLink fail", e);
        }
    }

    // 补全短链的显示短链
    private void completeDisplayedShortLink(List<SmsSendTask> smsSendTasks) {
        for (SmsSendTask task : smsSendTasks) {
            if (StringUtils.isNotEmpty(task.getShortLink()) && StringUtils.isNotEmpty(task.getContent())) {
                Matcher matcher = PATTERN.matcher(task.getContent());
                // 任务的短链不为空，从其文案中提取出显示短链
                if (matcher.find()) {
                    String displayedShortLink = matcher.group();
                    if (StringUtils.isNotEmpty(task.getShortLink())) {
                        String shortLink = task.getShortLink();
                        ShortLink sl = shortLinkRepository.findByShortLink(shortLink);
                        if (sl != null && StringUtils.isEmpty(sl.getDisplayedShortLink())) {
                            sl.setDisplayedShortLink(displayedShortLink);
                            shortLinkRepository.save(sl);
                        }
                    }
                }
            }
        }
    }

    // 补全发送任务短链
    private void completeShortLink(List<SmsSendTask> smsSendTasks) {
        for (SmsSendTask task : smsSendTasks) {
            if (StringUtils.isEmpty(task.getShortLink())) {
                if (StringUtils.isNotEmpty(task.getContent())) {
                    Matcher matcher = PATTERN.matcher(task.getContent());
                    if (matcher.find()) {
                        String displayedShortLink = matcher.group();
                        ShortLink sl = null;
                        // todo 不确定对应哪条短链，需要事先确定！！！！
                        List<ShortLink> list = shortLinkRepository.findByDisplayedShortLinkOrderByCtimeDesc(displayedShortLink);
                        if (CollectionUtils.isNotEmpty(list)) {
                            sl = list.get(0);
                        }
                        if (sl != null) {
                            task.setShortLink(sl.getShortLink());
                            smsSendTaskRepository.save(task);
                        }
                    }
                }
            }
        }
    }
}
